from django.http import JsonResponse
from django.shortcuts import render
from comment.errorcode import *
from .controller import *

import logging

# Create your views here.
from utils.responseutils import format_response, RESP_TITLE, err_response


@format_response
def demo(request):
    active = request.GET.get("active")
    try:
        params = json.loads(request.body)
        response_active = {
            "main": demo_main,
            "menus": demo_menus,
            "read": demo_read,
            "editReady": demo_edit_ready,
            "editUpdate": demo_edit_update,
            "create": demo_create,
            "superme":demo_superme,
        }.get(active)(request,params)
    except Exception as e:
        logging.error(e)
        return err_response(RESP_ERR, "响应为空")
    if response_active is None:
        return err_response(RESP_ERR, "响应为空")
    resp = dict(zip(RESP_TITLE, response_active))
    return JsonResponse(resp)


# @format_response
# def fault(request):
#     active = request.GET.get("active")
#     response_active = {}.get(active)(request)
#     if response_active is None:
#         return err_response(request, ErrorCodes)
#     resp = dict(zip(RESP_TITLE, response_active))
#     return JsonResponse(resp)
#
#
# @format_response
# def democls(request):
#     active = request.GET.get("active")
#     response_active = {}.get(active)(request)
#     if response_active is None:
#         return err_response(request, ErrorCodes)
#     resp = dict(zip(RESP_TITLE, response_active))
#     return JsonResponse(resp)
#
#

@format_response
def demofile(request):
    active = request.GET.get("active")
    try:
        params = json.loads(request.body)
        response_active = {
            "update": demo_file_update,
            "download": demo_file_download
        }.get(active)(request,params)
    except Exception as e:
        logging.error(e)
        return err_response(RESP_ERR, "响应为空")
    if response_active is None:
        return err_response(RESP_ERR, "响应为空")
    #resp = dict(zip(RESP_TITLE, response_active))
    return response_active


@format_response
def collect(request):
    active = request.GET.get("active")
    try:
        params = json.loads(request.body)
        response_active = {
            "create": collect_demo,
        }.get(active)(request,params)
    except Exception as e:
        logging.error(e)
        return err_response(RESP_ERR, "响应为空")
    if response_active is None:
        return err_response(RESP_ERR, "响应为空")
    resp = dict(zip(RESP_TITLE, response_active))
    return JsonResponse(resp)

# @format_response
# def scene(request):
#     active = request.GET.get("active")
#     response_active = {}.get(active)(request)
#     if response_active is None:
#         return err_response(request, ErrorCodes)
#     resp = dict(zip(RESP_TITLE, response_active))
#     return JsonResponse(resp)
#
#
# @format_response
# def product(request):
#     active = request.GET.get("active")
#     response_active = {}.get(active)(request)
#     if response_active is None:
#         return err_response(request, ErrorCodes)
#     resp = dict(zip(RESP_TITLE, response_active))
#     return JsonResponse(resp)
